package com.xbongbong.formula.qlexpress.operator.math;

import com.ql.util.express.Operator;
import com.ql.util.express.OperatorOfNumber;
import com.xbongbong.formula.constant.Constant;
import com.xbongbong.paas.toolbox.util.MathUtil;

import java.util.Objects;

/**
 * @author: wufeng
 * @date 2020/02/04
 * @desrcption: 精确计算和
 */
public class OperatorAdd extends Operator {


    public OperatorAdd(String name){
        this.name = name;
    }

    public OperatorAdd(String aAliasName, String aName, String errorInfo){
        this.aliasName = aAliasName;
        this.name = aName;
        this.errorInfo = errorInfo;
    }
    @Override
    public Object executeInner(Object[] list) throws Exception {
        Integer accuracy = Constant.EIGHT;
        if (list.length == Constant.SCALE) {
            accuracy = Integer.valueOf(list[2].toString());
        }
        Object sumResult = OperatorOfNumber.add(list[0], list[1], true);
        if (Objects.isNull(sumResult)) {
            return null;
        }
        if (list.length == Constant.LENGTH) {
            return sumResult;
        }
        return MathUtil.ceil(sumResult, accuracy);
    }
}
